public with sharing class AutoController {
	
	public Map <Id,Auto__c> mapAuto{get;set;}
	//public transient Auto__c objAuto{get;set;}
	public Apexpages.StandardController ctrl;
	public String selectedEntry{get;set;}
	public List<Auto__c> lAuto{get;set;}
	public boolean editMode{get;set;}
	
	
	//Constructor
	public AutoController(ApexPages.StandardController theController){
		
		editMode = false;
		ctrl = theController;
		
		
		try{
			mapAuto = new Map<Id,Auto__c>([
							SELECT Name,Marke__c,Modell__c,Preis__c,Beschreibung__c FROM Auto__c
			]);
		}catch(Exception ex){
			Apexpages.addMessages(ex);
		}
		
		lAuto = mapAuto.values();
		
		//getEntries();
		
		
		
	}//END AutoController()
	
	public void editEntry(){
		
		editMode = true;
		Auto__c autoSelectedEntry = mapAuto.get(selectedEntry);
		Auto__c currentPageValues = (Auto__c)ctrl.getRecord();
		
		currentPageValues.Name = autoSelectedEntry.Name;
		currentPageValues.Marke__c  = autoSelectedEntry.Marke__c;
		currentPageValues.Modell__c = autoSelectedEntry.Modell__c;
		currentPageValues.Preis__c = autoSelectedEntry.Preis__c;
		
		
	}
	
	public PageReference updateEntry(){
		Auto__c selectedAutoEntry = mapAuto.get(selectedEntry);
		Auto__c pageValues = (Auto__c) ctrl.getRecord();
		
		selectedAutoEntry.Name = pageValues.Name;
		selectedAutoEntry.Marke__c = pageValues.Marke__c;
		selectedAutoEntry.Modell__c = pageValues.Modell__c;
		selectedAutoEntry.Preis__c = pageValues.Preis__c;
		
		update selectedAutoEntry;
		
		PageReference currentPage = new PageReference('/apex/AutoPage');

        currentPage.setRedirect(true);
        return currentPage;
	}
	
	public PageReference deleteEntry(){

		delete mapAuto.get(selectedEntry);
		
		PageReference currentPage = new PageReference('/apex/AutoPage');

        currentPage.setRedirect(true);
        return currentPage;
	}
	
	public Pagereference save(){
		//try{
		
			Auto__c pageValues = (Auto__c) ctrl.getRecord();
			
			Auto__c auto2Insert = new Auto__c(
				Name = pageValues.Name,
				Marke__c = pageValues.Marke__c,
				Modell__c = pageValues.Modell__c,
				Preis__c = pageValues.Preis__c
			);
	
			insert auto2Insert;
			
			PageReference currentPage = new PageReference('/apex/AutoPage');

	        currentPage.setRedirect(true);
	        return currentPage;
		//}catch(Exception ex){
			//Apexpages.addMessages(ex);
			//return null;
		//}
	}
	
	public List<SelectOption> getEntries(){
		List<SelectOption> resultList = new List<SelectOption>();
		
		for(Auto__c a : mapAuto.values()){
			resultList.add(new SelectOption(a.Id,a.Name+' '+a.Marke__c+' '+a.Modell__c));
		}
		
		return resultList;
	}
	
	public static testmethod void myUnitTest(){
		Auto__c testAuto = new Auto__c(
			Name='TestAuto',
			Marke__c = 'TestMarke',
			Modell__c = 'TestModell',
			Preis__c = 100.00
		);
		
		insert testAuto;
		
		ApexPages.StandardController controller = new ApexPages.StandardController(testAuto);
		
		AutoController myPageCtrl = new AutoController(controller);
		
		myPageCtrl.getEntries();
		
		myPageCtrl.selectedEntry = testAuto.Id;
		
		myPageCtrl.editEntry();
		
		myPageCtrl.updateEntry();
		
		myPageCtrl.save();
		
		myPageCtrl.deleteEntry();
		
	}

}